package com.data.modules.online.config.service.p020a;

import com.data.common.util.ConvertUtils;
import com.data.modules.online.cgform.extend.ExtendJsonKey;
import com.data.modules.online.cgform.util.CgformUtil;
import com.data.modules.online.cgform.util.OnlFormShowType;
import com.data.modules.online.config.executor.ColumnMeta;
import com.data.modules.online.config.executor.DataBaseConst;
import com.data.modules.online.config.service.DbTableHandleI;

public class DbTableSQLServerHandleImpl implements DbTableHandleI {
    @Override
    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD  " + getAddColumnDataType(columnMeta) + ";";
    }

    @Override
    public String getReNameFieldName(ColumnMeta columnMeta) {
        return "  sp_rename '" + columnMeta.getTableName() + "." + columnMeta.getOldColumnName() + "', '" + columnMeta.getColumnName() + "', 'COLUMN';";
    }

    @Override
    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return " ALTER COLUMN  " + getAlterColumnDataType(cgformcolumnMeta, datacolumnMeta) + ";";
    }

    @Override
    public String getMatchClassTypeByDataType(String dataType, int digits) {
        if (dataType.equalsIgnoreCase(DataBaseConst.f477l) || dataType.equalsIgnoreCase("nvarchar")) {
            return DataBaseConst.f474i;
        }
        if (dataType.equalsIgnoreCase("float")) {
            return "double";
        }
        if (dataType.equalsIgnoreCase("int")) {
            return "int";
        }
        if (dataType.equalsIgnoreCase("Date") || dataType.equalsIgnoreCase("Datetime")) {
            return OnlFormShowType.f320d;
        }
        if (dataType.equalsIgnoreCase("numeric")) {
            return "bigdecimal";
        }
        if (dataType.equalsIgnoreCase("varbinary") || dataType.equalsIgnoreCase("image")) {
            return "blob";
        }
        if (dataType.equalsIgnoreCase(ExtendJsonKey.f91e) || dataType.equalsIgnoreCase("ntext")) {
            return ExtendJsonKey.f91e;
        }
        return "";
    }

    @Override
    public String dropTableSQL(String tableName) {
        return " DROP TABLE " + tableName + " ;";
    }

    @Override
    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }

    private String getAlterColumnDataType(ColumnMeta aVar, ColumnMeta aVar2) {
        String str;
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            StringBuilder append = new StringBuilder().append(aVar.getColumnName()).append(" nvarchar(").append(aVar.getColumnSize()).append(") ");
            if ("Y".equals(aVar.getIsNullable())) {
                str = "NULL";
            } else {
                str = "NOT NULL";
            }
            return append.append(str).toString();
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            return aVar.getColumnName() + " datetime " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            return aVar.getColumnName() + " int " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("double")) {
            return aVar.getColumnName() + " float " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("bigdecimal")) {
            return aVar.getColumnName() + " numeric(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            return aVar.getColumnName() + " ntext " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("blob")) {
            return aVar.getColumnName() + " image";
        } else {
            return "";
        }
    }

    private String getAddColumnDataType(ColumnMeta aVar) {
        String str;
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            StringBuilder append = new StringBuilder().append(aVar.getColumnName()).append(" nvarchar(").append(aVar.getColumnSize()).append(") ");
            if ("Y".equals(aVar.getIsNullable())) {
                str = "NULL";
            } else {
                str = "NOT NULL";
            }
            return append.append(str).toString();
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            return aVar.getColumnName() + " datetime " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            return aVar.getColumnName() + " int " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("double")) {
            return aVar.getColumnName() + " float " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("bigdecimal")) {
            return aVar.getColumnName() + " numeric(" + aVar.getColumnSize() + CgformUtil.f231E + aVar.getDecimalDigits() + ") " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase(ExtendJsonKey.f91e)) {
            return aVar.getColumnName() + " ntext " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("blob")) {
            return aVar.getColumnName() + " image";
        } else {
            return "";
        }
    }

    private String m462b(ColumnMeta aVar) {
        if (aVar.getColunmType().equalsIgnoreCase(DataBaseConst.f474i)) {
            return aVar.getColumnName() + " nvarchar(" + aVar.getColumnSize() + ") " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase(OnlFormShowType.f320d)) {
            return aVar.getColumnName() + " datetime " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (aVar.getColunmType().equalsIgnoreCase("int")) {
            return aVar.getColumnName() + " int " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (!aVar.getColunmType().equalsIgnoreCase("double")) {
            return "";
        } else {
            return aVar.getColumnName() + " float " + ("Y".equals(aVar.getIsNullable()) ? "NULL" : "NOT NULL");
        }
    }

    @Override
    public String getCommentSql(ColumnMeta columnMeta) {
        StringBuffer stringBuffer = new StringBuffer("EXECUTE ");
        if (ConvertUtils.isEmpty(columnMeta.getOldColumnName())) {
            stringBuffer.append("sp_addextendedproperty");
        } else {
            stringBuffer.append("sp_updateextendedproperty");
        }
        stringBuffer.append(" N'MS_Description', '");
        stringBuffer.append(columnMeta.getComment());
        stringBuffer.append("', N'SCHEMA', N'dbo', N'TABLE', N'");
        stringBuffer.append(columnMeta.getTableName());
        stringBuffer.append("', N'COLUMN', N'");
        stringBuffer.append(columnMeta.getColumnName() + CgformUtil.f229C);
        return stringBuffer.toString();
    }

    @Override
    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    @Override
    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName + CgformUtil.f293d + tableName;
    }

    @Override
    public String countIndex(String indexName, String tableName) {
        return "SELECT count(*) FROM sys.indexes WHERE object_id=OBJECT_ID('" + tableName + "') and NAME= '" + indexName + CgformUtil.f229C;
    }
}
